package com.dd.machine.main.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.dd.machine.R;
import com.dd.machine.base.activity.BaseActivity;
import com.dd.machine.base.utils.StringUtils;
import com.dd.machine.main.desktop.DeskTopActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.dd.machine.base.net.URLManager.CARDPHONEBIND;

/**
 * Created by Administrator on 2017/6/30.
 */

public class LoginActivity extends BaseActivity {


    @Bind(R.id.back)
    RelativeLayout back;
    @Bind(R.id.bind_cardnum_tv)
    TextView bindCardnumTv;
    @Bind(R.id.bind_phoneNum_tv)
    EditText bindPhoneNumTv;
    @Bind(R.id.bind_btn)
    Button bindBtn;

    Bundle bundle;

    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_bind);
        context = LoginActivity.this;
        ButterKnife.bind(this);
        bundle = getIntent().getExtras();
        if (bundle!=null) {
            bindCardnumTv.setText(bundle.getString("number"));
        }else {
            bindCardnumTv.setText("1111111111");
        }

    }


    @OnClick({R.id.back, R.id.bind_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                Intent intent = new Intent(LoginActivity.this, DeskTopActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.back_anim, R.anim.backback_anim);
                break;
            case R.id.bind_btn:
                bind();
                break;
        }
    }

    private void bind() {
        if (StringUtils.isTel(bindPhoneNumTv.getText().toString())) {
            String cardNum = bindCardnumTv.getText().toString();
            String cardPhone = bindPhoneNumTv.getText().toString();

            //绑定
            cardBind(cardNum,cardPhone);
        }else {
            Toast.makeText(LoginActivity.this,"手机号格式不正确",Toast.LENGTH_SHORT).show();
        }
    }

    private void cardBind(String cardNum,String mobile){
        OkHttpUtils.post()
                .url(CARDPHONEBIND)
                .addParams("mobile",mobile)
                .addParams("cardNo",cardNum)
                .addParams("cardType","common")
                .addParams("equipmentNo","JINANDINGGAO")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("--carBind-erroe",e.getMessage());

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("--carBind-response",response);
                        json(response,context);
                    }
                });
    }

    private void json(String response, Context context) {
        try {
            JSONObject object = new JSONObject(response);
            JSONObject jsonObject = object.optJSONObject("userInfo");
            String  userId = jsonObject.optString("id");
            Intent intent = new Intent(context, GoodsKindActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("userId",userId);
            intent.putExtras(bundle);
            startActivity(intent);
            finish();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}
